<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .contain {
            width: 300px;
            height: 150px;
            border: 1px solid #cdcdcd;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            border-radius: 5px;
            box-shadow: 0px 0px 3px 3px #cdcdcd;
        }

        .contain div {
            margin-top: 30px;
        }

        .contain input {
            height: 23px;
            border: 1px solid #cdcdcd;
            outline: none;
            text-indent: 4px;
        }

        button {
            width: 200px;
            height: 30px;
            border: 0;
        }

        p {
            color: rgb(214, 0, 0);
            opacity: 0;
            margin-bottom: 30px;
        }
    </style>
</head>

<body>
    <div class="contain">
        <div>用户名：<input type="text" id="username"></div>
        <p>*用户名已存在</p>
        <button>注册</button>
    </div>

    <script>
        const username = document.querySelector("#username");
        const tip = document.querySelector("p");
        const button = document.querySelector("button");

        // 当input失去焦点时，判断是否重复或没填
        username.addEventListener('blur', () => {
            let value = username.value.trim();
            // 如果为空则提示为空，不为空检验是否重复
            if (value === "") {
                tip.textContent = "*请输入用户名";
                tip.style.opacity = 1;
                tip.style.color = 'rgb(214, 0, 0)';
            } else {
                // 发送post请求
                fetch("http://192.168.192.176:3000/test", {
                    method: "POST",
                    headers: {
                        "Content-type": "application/json"
                    },
                    body: JSON.stringify({
                        username: value
                    })
                }).then(result => {
                    return result.json();
                }).then(result => {
                    console.log(result);
                    // 根据返回的判断用户名是否存在
                    if (!result.status) {
                        tip.textContent = "*用户名已存在";
                        tip.style.opacity = 1;
                    } else {
                        tip.style.opacity = 0;
                    }
                })
            }
        })

        // 当点击注册时，添加用户名
        button.addEventListener('click', () => {
            let value = username.value.trim();
            if (value === "") {
                tip.textContent = "*请输入用户名";
                tip.style.opacity = 1;
                tip.style.color = 'rgb(214, 0, 0)';
            } else {
                // 发送post请求
                fetch("http://192.168.192.176:3000/user", {
                    method: "POST",
                    headers: {
                        "Content-type": "application/json"
                    },
                    body: JSON.stringify({
                        username: value
                    })
                }).then(result => {
                    return result.json();
                }).then(result => {
                    // 根据返回的判断用户名是否存在
                    if (!result.status) {
                        tip.textContent = "*用户名已存在";
                        tip.style.opacity = 1;
                        tip.style.color = 'rgb(214, 0, 0)';
                    } else {
                        tip.style.opacity = 1;
                        tip.textContent = "*注册成功";
                        tip.style.color = 'rgb(81,185,38)';
                    }
                })
            }
        })
    </script>
</body>

</html>